package com.blog.controller.frontpage;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blog.controller.base.BaseServlet;
import com.blog.entity.BlogPostEntity;
import com.blog.entity.UserLoginEntity;
import com.blog.service.frontpage.PersonalCenterService;
import com.blog.utils.Util;

@WebServlet("/blogpost")
public class BlogPostServlet extends BaseServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		UserLoginEntity user=(UserLoginEntity )req.getSession().getAttribute("user");
		int userid=user.getUser_id();
		BlogPostEntity entity=new BlogPostEntity();
		PersonalCenterService service=PersonalCenterService.getInstance();
		String title=req.getParameter("title");
		String types=req.getParameter("types");
		String content=req.getParameter("content");
		Util util=new Util();
		Timestamp time=util.getTimestamp();
		String posttype=req.getParameter("blogpost");
		entity.setContent(content);
		entity.setTime(time);
		entity.setTitle(title);
		entity.setTypes(Integer.parseInt(types));
		entity.setWriterid(userid);
		if(posttype.equals("发布")) {
			entity.setStatus(0);
			System.out.println("保存成功");
		}
		else {
			entity.setStatus(1);
			System.out.println("存草稿");
		}
		service.saveBlog(entity);
		forward("frontpage/newessay_edit");
		
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
}